using Godot;
using System;

public partial class ThrowSilk : Node, StateNode
{
    public Enemy enemy;

    public Silk silk;

    public override void _Ready()
    {
        enemy = GetNode<Enemy>("/root/Main/Enemy");

        silk = enemy.GetNode<Silk>("Weapons/Silk");

        enemy.GetNode<AnimatedSprite2D>("AnimaEnemy").AnimationFinished += OnEndThrowSilk;
    }

    private void OnEndThrowSilk()
    {
        if(enemy.GetNode<AnimatedSprite2D>("AnimaEnemy").Animation == "throw_silk")
        {
            if (!enemy.IsOnFloor() && enemy.Health > 5 && MyUtil.RangeRandom(0, 100) <= 25)
                enemy.SwitchTo("Aim");
            else if (!enemy.IsOnFloor())
                enemy.SwitchTo("Fall");
            else
                enemy.SwitchTo("Idle");
        }
    }

    public void OnEnter()
    {
        enemy.GetNode<AnimatedSprite2D>("AnimaEnemy").Animation = "throw_silk";
        enemy.enableGravity = false;

        silk.OnThrowSilk();
    }

    public void OnExit()
    {
        enemy.enableGravity = true;
    }

    public void OnUpdate(float delta)
    {
        if (enemy.Health <= 0)
        {
            enemy.SwitchTo("Dead");
        }
    }
}
